package kr.co.mcshop.product;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.mcshop.optionDetail.OptionDetailService;
import kr.co.mcshop.qna.QnA;
import kr.co.mcshop.qna.QnAService;
import kr.co.mcshop.review.ReviewBoard;
import kr.co.mcshop.review.ReviewBoardService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping(value = "/product")
@Controller
public class ProductController {
	@Autowired
	ProductService ps;

	@Autowired
	OptionDetailService ods;
	@Autowired
	private ReviewBoardService board;
	@Autowired
	private QnAService qnaService;

	@RequestMapping(value = "/main")
	public String InsertForm(Model model, @RequestParam Map map) {
		List list = (List) ps.getAllProduct(map);

		model.addAttribute("str", list);
		return "main";
	}

	@RequestMapping(value = "/ProductDetail")
	public String ProductDetail(Model model, @RequestParam Map map) {
		List list = ps.getAllProduct(map);
		// 선택된 상품의 상세정보를 가져옴.

		HashMap<Object, Object> detailMap = (HashMap<Object, Object>) list
				.get(0);

		Integer opt1 = (Integer) detailMap.get("opt1");
		Integer opt2 = (Integer) detailMap.get("opt2");
		List detailList1 = ods.getAllOptionDetail(opt1);
		List detailList2 = ods.getAllOptionDetail(opt2);

		List<ReviewBoard> BOARD = board.getAllBoard(map);
		model.addAttribute("BOARD", BOARD);

		List<QnA> list1 = qnaService.selectqna(map);
	      model.addAttribute("qnaList", list1);
	      
		// 선택된 상품의 옵션목록을 가져오기 위한 리스트;
		System.out.println(detailList1);
		System.out.println(detailList2);
		System.out.println(detailMap.get("NAME"));
		String[] Image = detailMap.get("IMAGE").toString().split("[/]");
		System.out.println(Image);
		model.addAttribute("detailList1", detailList1);
		model.addAttribute("detailList2", detailList2);
		model.addAttribute("image", Image);
		model.addAttribute("product", list.get(0));
		return "ProductDetail";
	}
	
	@RequestMapping(value="/categoryProduct")
	public String categoryProduct(@RequestParam Map map, Model model){
		List<Map> categoryProductList = (List<Map>)ps.getSelectedProduct(map);
		System.out.println(map);
		System.out.println(categoryProductList);
		model.addAttribute("productList", categoryProductList);
		return "categoryProduct";
	}

}
